﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using PerfumeShopManagement.Model;

namespace PerfumeShopManagement.Controller
{
    public class ProductController : AbstractBaseController
    {
        public override Object GetObjectById(Object id)
        {
            throw new NotImplementedException();
        }

        public override List<Object> GetAllObjects()
        {
            var result = (from p in _context.Products
                          select p);
            if (result != null)
                return result.ToList<object>();

            return new List<object>();
        }

        public override List<Object> GetObjectsByCriteria(Object obj)
        {
            throw new NotImplementedException();
        }

        public override bool Save(Object obj)
        {
            Product product = (Product)obj;
            if (_context.Products.Contains(product)) // Update
            {
                Product productRow = _context.Products.Single(
                    p => p.ProductId == product.ProductId);
                productRow.ProductName = product.ProductName;
                productRow.Price = product.Price;
                productRow.Inventory = product.Inventory;
                productRow.DetailInfo = product.DetailInfo;
                productRow.CategoryId = product.CategoryId;
                _context.SubmitChanges();
            }
            else // Insert
            {
                _context.Products.InsertOnSubmit(product);
                _context.SubmitChanges();
            }

            return true;
        }

        public override bool Delete(Object obj)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteById(Object id)
        {
            bool deleted = false;
            int productId = (int)id;
            var result = (from p in _context.Products
                          where p.ProductId.Equals(productId)
                          select p).FirstOrDefault();
            if (result != null)
            {
                _context.Products.DeleteOnSubmit((Product)result);
                _context.SubmitChanges();
                deleted = true;
            }

            return deleted;
        }
    }
}
